package com.wobangkj.common.service.impl;

import com.wobangkj.common.model.crud.Tag;
import com.wobangkj.common.repository.TagRepository;
import com.wobangkj.common.service.TagService;
import com.wobangkj.tool.api.result.Result;
import com.wobangkj.tool.util.sql.CrudUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.wobangkj.tool.util.RestUtil.copyNonNullProperties;

@Service
@Slf4j
public class TagServiceImpl implements TagService{
    @Autowired
    private TagRepository repository;

    @Override
    public Object update(Tag tag) {

        Tag existData = repository.findById(tag.getId()).get();
        copyNonNullProperties(tag, existData);
        return CrudUtil.update(existData, repository);
    }

    @Override
    @SuppressWarnings("Duplicates")
    public Object create(Tag tag) {
        try {
            Tag createData = repository.save(tag);
            // 使用 uuid 作为源 token
            //String     token = UUID.randomUUID().toString().replace("-", "");
            //TokenModel model = new TokenModel(createData.getId(), token);
            // 30 分钟有效期
            //cacheManager.set(model.getToken(), new CacheModel(TOKEN_MINUTE, model));
            return Result.GetMapData(Result.CodeCreate, Result.MsgCreate);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }

    @Override
    public List<Map> list(String[] testint) {
        List<Map> idsStr = new ArrayList<>();
        for (int i = 0; i < testint.length; i++) {
            idsStr=repository.selectByTestint(testint[i]);
        }
        return idsStr;
    }

    @Override
    public List<Tag> findByNameLike(String tag_name) {
        return repository.findByNameLike(tag_name);
    }

}
